//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2006 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   USER
// AUTHOR           :   William Xu 
// DATE CREATE      :   2006/06/27
// DESCRIPTION      :   a COMMON DAO file for the function, USER
//                  :   
//                  :   
//                  :   
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2006/06/27   William Xu      1.0                 source is generated.
//
//
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package com.erp.app.admin.userSetup.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.erp.app.admin.userSetup.dao.UserCommonDao;
import com.erp.app.admin.userSetup.bizLogic.UserCommonUtil;

import com.erp.app.common.ProjectConstants;
import com.erp.framework.model.criteria.SimpleBaseSrchCriteria;
import com.erp.framework.model.vo.SimpleBaseVo;
import com.erp.app.admin.userSetup.criteria.UserSrchCriteria;

/**
* <p>A <strong>CommonDao</strong> provides database access functions
*  for delegate classes.
*
* @author          William Xu 
* @version 1.0     2006/06/27
* @since           FrameWork 1.1
*
*/
public class UserCommonDao {

	private UserCommonUtil util = new UserCommonUtil();

	/**
	* Returns the SQL select statement.
	* @param         obj     the searching criteria
	* @return        a string containing sql query
	*
	* @author     William Xu 
	* @since      Framework 1.1
	*/
	public static String getHdrSqlQuery(SimpleBaseSrchCriteria obj) {

		UserSrchCriteria criteria = (UserSrchCriteria) obj;
		String sql = "";
		String whereClause = hdrToWhereClause(criteria);
		String groupByClause = "";
		String orderByClause = "ORDER BY USER_CODE, COMPANY_CODE";

		if(!criteria.isSearchResult()){
			sql = "SELECT USER_CODE, COMPANY_CODE, USER_NAME, EMAIL, PASSWORD, "
			+ "ROLE_GROUP_ID, GROUP_LEVEL, ACTIVATION, CREATE_USER, CREATE_DATE, "
			+ "UPDATE_USER, UPDATE_DATE, USER_GROUP_CODE, DEPT, CONTENT_TYPE, "
			+ "SIGNATURE, SALESMAN_FLAG, APPROVER, QA_HANDLER, QC_HANDLER, "
			+ "GMM, DIVISION "
			+ "FROM T_USER "
			+ whereClause
			+ groupByClause
			+ orderByClause;
		} else {
			throw new RuntimeException("Please implement query for the search screen");
		}

		return sql;
	}

	/**
	* Sets the values of a result set to a vo for a search screen
	* @param         rs   the incoming result set
	* @return        a vo containing the result set values
	*
	* @author     William Xu 
	* @since      Framework 1.1
	*/
	public static SimpleBaseVo getHdrSearchResultSetValues(ResultSet rs) throws SQLException {

		throw new RuntimeException("Please get the ResultSet values for the search screen");

	}

	/**
	* Returns the where clause of a SQL select statement.
	* @param         obj     the searching criteria
	* @return        a string containing the where clause
	*
	* @author     William Xu 
	* @since      Framework 1.1
	*/
	public static String hdrToWhereClause(UserSrchCriteria crit) {

		UserSrchCriteria criteria = (UserSrchCriteria) crit;
		StringBuffer whereClause = new StringBuffer();

		if (criteria.getSrchCompanyCode() != null) {
			whereClause.append(whereClause.length()== 0? "WHERE ": "AND ");
			whereClause.append(" COMPANY_CODE = '" + criteria.getSrchCompanyCode().trim().toUpperCase()+"' ");
		}

		/*
		if (criteria.getSrchProgram() != null) {
			whereClause.append(whereClause.length() == 0 ? " WHERE " : " AND ");
			whereClause.append(" SALES_CONTRACT_NO IN ");
			whereClause.append(" (SELECT SALES_CONTRACT_NO FROM T_SALES_CONTRACT_DTL");
			if (criteria.getSrchProgram() != null) {
				whereClause.append(" WHERE PROGRAM ='" + criteria.getSrchProgram().toUpperCase().trim() + "'");
			}
			whereClause.append(")");
		}
		if (criteria.getSrchSalesContractNoFr() != null) {
			whereClause.append(whereClause.length() == 0 ? "WHERE " : "AND ");
			whereClause.append("SALES_CONTRACT_NO >= '"
				+ criteria.getSrchSalesContractNoFr().toUpperCase().trim() + "' ");
		}
		if (criteria.getSrchSalesContractNoTo() != null) {
			whereClause.append(whereClause.length() == 0 ? "WHERE " : "AND ");
			whereClause.append("SALES_CONTRACT_NO <= '"
				+ criteria.getSrchSalesContractNoTo().toUpperCase().trim() + "' ");
		}
		if (criteria.getSrchPoDateFr() != null) {
			whereClause.append(whereClause.length() == 0 ? "WHERE " : "AND ");
			whereClause.append(" PO_DATE >= TO_DATE('"
				+ criteria.getSrchPoDateFr() + "', '"
				+ ProjectConstants.ORACLE_INPUT_DATE_FORMAT + "') ");
		}
		if (criteria.getSrchPoDateTo() != null) {
			whereClause.append(whereClause.length() == 0 ? "WHERE " : "AND ");
			whereClause.append(" PO_DATE <= TO_DATE('"
				+ criteria.getSrchPoDateTo() + "', '"
				+ ProjectConstants.ORACLE_INPUT_DATE_FORMAT + "') ");
		}
		*/

		return whereClause.toString();
	}

}
